<?php

function reequipment_userapi_getrelcycles($args)
{
    extract($args);

    /* Fields in table.
    	maintenance_id
        item_id
        equipment_id
        cycles
        date_added
   */

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['rel_cycles'];
    $join_items = $xartable['reitems'];
    $join_equipment = $xartable['reequipment'];
    $join_model = $xartable['remodels'];
    $join_maintenance = $xartable['remaintenance'];

    $query = "SELECT DISTINCT c.maintenance_id,
					 c.item_id,
        			 c.equipment_id,
        			 c.cycles,
        			 c.date_added,
        			 i.part_number,
        			 i.description,
        			 e.serial_number,
        			 e.cycles as equipment_cycles,
        			 m.model_name,
        			 ma.issue,
        			 ma.resolution"
    		. " FROM $table c"
    		
    		. " LEFT OUTER JOIN $join_equipment e"
    		. " ON c.equipment_id = e.equipment_id"     		
    		
    		. " LEFT OUTER JOIN $join_model m"
    		. " ON e.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $join_items i"
    		. " ON c.item_id = i.item_id"
    		
    		. " LEFT OUTER JOIN $join_maintenance ma"
    		. " ON c.maintenance_id = ma.maintenance_id";       		

    $where = array();
    $bind = array();
    
    if (!empty($item_id)) {
        $where[] = 'c.item_id = ?';
        $bind[] = (int)$item_id;
    }
    
    if (!empty($maintenance_id)) {
        $where[] = 'c.maintenance_id = ?';
        $bind[] = (int)$maintenance_id;
    }    
    
    if (!empty($equipment_id)) {
        $where[] = 'c.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }    
    
    if (!empty($model_id)) {
        $where[] = 'm.model_id = ?';
        $bind[] = (int)$model_id;
    }    

    if (!empty($cycles)) {
        $where[] = 'c.cycles BETWEEN e.cycles - 5000 AND e.cycles + 5000';
        $bind[] = (int)$cycles;
    }  
    
    // Figure out the dates when we need it.

    if (!empty($date_added)) {
        $where[] = 'c.date_added = ?';
        $bind[] = (string)$date_added;
    }    
    
    if (!empty($scheduled)) {
        $where[] = 'ma.schedule_id != ?';
        $bind[] = (int)0;
    }     

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY i.item_id";
    // return var_export($query);

    $result =& $dbconn->SelectLimit($query, 1000, 0, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($maintenance_id,
			 $item_id,
        	 $equipment_id,
        	 $cycles,
        	 $date_added,
        	 $part_number,
        	 $description,
        	 $serial_number,
        	 $equipment_cycles,
        	 $model_name,
        	 $issue,
        	 $resolution) = $result->fields;

        $tags[] = array(
        		'maintenance_id'	=> $maintenance_id,
                'item_id'      		=> $item_id,
                'equipment_id'      => $equipment_id,
                'cycles'			=> $cycles,
                'date_added' 		=> $date_added,
                'part_number'		=> xarVarPrepForDisplay($part_number),
                'description' 		=> xarVarPrepForDisplay($description),
                'serial_number'    	=> xarVarPrepForDisplay($serial_number),
                'equipment_cycles'			=> $equipment_cycles,
                'model_name'    	=> xarVarPrepForDisplay($model_name),
                'issue'    			=> xarVarPrepForDisplay($issue), 
                'resolution'    	=> xarVarPrepForDisplay($resolution)              
        );
    }

    $result->Close();
    return $tags;
}
?>